<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/5
 * Time: 13:46
 *
 * @property CI_DB_mysqli_driver db
 */


class Admin_model extends CI_Model
{
    public $table_name = 'admin';
    public $limit_rows = 10;

    public function admin_option($param = array()){


        $param['is_delete'] = 0;

        $query = $this->db->from('admin')->where($param)->get();

        $admin = array();
        foreach($query->result_array() as $row){
            $admin[$row['admin_id']] = $row['admin_name'];
        }


        return $admin;
    }

    /**
     * 验证登录
     *
     * @param $username string
     * @param $password string
     *
     * @return array|bool
     * author: walker
     * Date: 2019/11/6
     * Time: 15:11
     * Note: 修改is_del字段问题
     */
    public function validation_user($username, $password){
        if($password && $username){

            $query = $this->db->from('admin')
                ->where(array('admin_name' => $username, 'password' => $password, 'is_del' => 0))
                ->get();

            $user = $query->row_array();

            return $user;

        }else {
            return false;
        }
    }

    public function update_pass($pass, $old_pass){

        $pass = md5($pass);

        $this->db->update('admin', array(
            'password' => $pass,
        ), "admin_name = '{$this->session->admin_name}' and password = '{$old_pass}'");
    }

    //list展示页面
    public function method_list($is_show = 0){
        if($is_show){
            $this->db->where(['is_show' => 1]);
        }
        return $this->db->from('con_met')->get()->result_array();
    }

    //修改展示不展示
    public function is_show($id){
        $is_show = $this->db->from('con_met')->where(['id' => $id])->select('is_show')->get()->row_array();
        if($is_show['is_show'] == 1){
            $this->db->where(['id' => $id])->update('con_met', ['is_show' => 2]);
        }else {
            $this->db->where(['id' => $id])->update('con_met', ['is_show' => 1]);
        }
        return 1;
    }

    //添加
    public function method_add($data){
        return $this->db->insert('con_met', $data);
    }

    //修改
    public function method_save($data){
        return $this->db->where(['id' => $data['id']])->update('con_met', $data);
    }

    //修改权限
    public function authorization($data){
        $authorization = implode(',', $data['method']);
        return $this->db->where(['admin_id' => $data['admin']])->update('admin', ['authorization' => $authorization]);
    }

    //查询单条
    public function method_find($id){
        return $this->db
            ->from('con_met')
            ->where(['id' => $id])
            ->get()->row_array();
    }


    /**
     * 添加部门
     *
     * @param $data
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 14:54
     * Note:
     */
    public function department_add($data){

        return $this->db->insert('department', $data);
    }

    /**
     * 修改部门
     *
     * @param $id
     * @param $data
     *
     * @return bool
     * author: walker
     * Date: 2019/11/8
     * Time: 9:54
     * Note:
     */
    public function update_department($id, $data){
        return $this->db->where('id', $id)->update('department', $data);
    }

    /**
     * 根据部门名称获取id
     *
     * @param $name
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:17
     * Note:
     */
    public function department_get_id($name){
        return $this->db->where('department_name', $name)->select('id,level')->get('department')->row_array();

    }

    /**
     * 获取父级等级
     *
     * @param $pid
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 18:38
     * Note:
     */
    public function get_parent_level($pid){
        return $this->db->where('id', $pid)->select('level')->get('department')->row_array();
    }

    /**
     * 获取部门列表
     *
     * @param array $param
     * @param int $start
     * @param int $pageNum
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:43
     * Note:
     */
    public function get_department_list($param = array(), $start = 0, $pageNum = 10){
        $this->db->from('department')
            ->where('is_del', 0)
            ->order_by('id desc')
            ->limit($pageNum, $start);
        if($param['department_name']){
            $this->db->where("department_name LIKE '%{$param['department_name']}%'");
        }
        $query = $this->db->get();
        return $query->result_array();

    }

    /**
     * 通过id检查是否存在
     *
     * @param $id
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:46
     * Note:
     */
    public function get_department_by_id($id){
        return $this->db->from('department')->where('id', $id)->select("*")->get()->result_array();
    }

    /**
     * 删除部门
     *
     * @param $id
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 16:31
     * Note:
     */
    public function del_department($id){
        $data = array();
        $data['is_del'] = 1;
        return $this->db->where('id', $id)->update('department', $data);
    }

    /**
     * 修改状态
     *
     * @param $id
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 17:52
     * Note:
     */
    public function edit_department_status($id){
        $status = $this->db->where('id', $id)->select('status')->get('department')->row_array();
        $data = array();
        $data['status'] = !$status['status'];
        $ids = $this->get_son_id($id);
        $ids[] = $id;

        return $this->db->where_in('id', $ids)->update('department', $data);
    }

    /**
     * 添加权限
     *
     * @param $data
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 14:54
     * Note:
     */
    public function add_role($data){

        return $this->db->insert('role', $data);
    }

    /**
     * 修改权限
     *
     * @param $id
     * @param $data
     *
     * @return bool
     * author: walker
     * Date: 2019/11/8
     * Time: 9:54
     * Note:
     */
    public function update_role($id, $data){
        return $this->db->where('id', $id)->update('role', $data);
    }

    /**
     * 通过id获取role信息
     *
     * @param $id
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:46
     * Note:
     */
    public function get_role_by_id($id){
        return $this->db->from('role')->where('id', $id)->select("*")->get()->result_array();
    }

    /**
     * 获取角色列表
     *
     * @param array $param
     * @param int $start
     * @param int $pageNum
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:43
     * Note:
     */
    public function get_role_list($param = array(), $start = 0, $pageNum = 10){
        $this->db->from('role')
            ->where('is_del', 0)
            ->order_by('id desc')
            ->limit($pageNum, $start);
        if($param['role_name']){
            $this->db->where("role_name LIKE '%{$param['role_name']}%'");
        }
        $query = $this->db->get();
        return $query->result_array();

    }


    /**
     * 删除角色
     *
     * @param $id
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 16:31
     * Note:
     */
    public function del_role($id){
        $data = array();
        $data['is_del'] = 1;
        return $this->db->where('id', $id)->update('role', $data);
    }

    /**
     * 修改角色状态
     *
     * @param $id
     *
     * @return bool
     * author: walker
     * Date: 2019/11/7
     * Time: 17:52
     * Note:
     */
    public function edit_role_status($id){
        $status = $this->db->where('id', $id)->select('status')->get('role')->row_array();
        $data = array();
        $data['status'] = !($status['status']);

        return $this->db->where('id', $id)->update('role', $data);
    }

    /**
     * 根据角色名称获取id
     *
     * @param $name
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 15:17
     * Note:
     */
    public function get_role_id($name){
        return $this->db->where('role_name', $name)->select('id')->get('role')->row_array();

    }

    /**
     * 获取子孙节点
     *
     * @param $id
     *
     * @return array
     * author: walker
     * Date: 2019/11/7
     * Time: 19:07
     * Note:
     */
    public function get_son_id($id){
        static $ids = array();
        $list = $this->db->where('pid', $id)->select('id')->get('department')->row_array();

        if(!empty($list)){
            foreach($list as $key => $val){

                $ids[] = $val;
                $this->get_son_id($val);
            }
        }
        return $ids;
    }




    /**
     *#############   通用数据库操作   #############
     */

    /**
     * 获取数据列表通用
     *
     * @param     $table
     * @param     $order
     * @param int $start
     * @param int $pageNum
     *
     * @return array
     * author: walker
     * Date: 2019/11/8
     * Time: 11:27
     * Note:
     */
    public function get_table_list($table, $order, $start = 0, $pageNum = 10){
        $this->db->from($table)
            ->where('is_del', 0)
            ->limit($pageNum, $start);
        if(!empty($order)){
            $this->db->order_by($order);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取单条数据通用
     *
     * @param        $table string
     * @param        $id    string
     * @param string $field
     *
     * @return array
     * author: walker
     * Date: 2019/11/8
     * Time: 11:29
     * Note:
     */
    public function get_table_row($table, $where = array(), $field = '*'){
        $this->db->from($table)
            ->select($field);

        if(!empty($where)){
            $this->db->where($where);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * 修改 通用
     *
     * @param $table
     * @param $where
     * @param $data
     * @return bool
     * author: walker
     * Date: 2019/11/8
     * Time: 14:10
     * Note:
     */
    public function update_row($table, $where, $data){
        $db = $this->db;
        if(!empty($where)){
            $db->where($where);
        }
        return $db->update($table, $data);
    }

    /**
     * 添加数据通用
     *
     * @param $table
     * @param $data
     *
     * @return bool
     * author: walker
     * Date: 2019/11/8
     * Time: 14:15
     * Note:
     */
    public function add_row($table, $data){
        return $this->db->insert($table, $data);
    }

    /**
     * 切换状态 通用
     * @param        $table
     * @param        $where
     * @param string $field
     *
     * @return bool
     * author: walker
     * Date: 2019/11/8
     * Time: 14:36
     * Note:
     */
    public function edit_row_status($table, $where, $field = 'status'){
        $status = $this->db->where($where)->select($field)->get($table)->row_array();
        $data = array();
        $data['status'] = !($status['status']);

        return $this->db->where($where)->update($table, $data);
    }

}
